from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, QSize, pyqtSignal
import os

class LeftSideBar(QWidget):
    # 定义信号
    file_explorer_toggled = pyqtSignal(bool)
    search_clicked = pyqtSignal()
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedWidth(50)
        self.setup_ui()
    
    def get_resource_path(self, relative_path):
        """获取资源文件的绝对路径，支持打包后的资源访问"""
        # 首先尝试从当前工作目录查找
        base_path = os.path.dirname(os.path.abspath(__file__))
        resource_path = os.path.join(base_path, "..", "resources", relative_path)
        
        if os.path.exists(resource_path):
            return resource_path
        
        # 如果找不到，尝试从打包后的资源目录查找
        resource_path = os.path.join("resources", relative_path)
        if os.path.exists(resource_path):
            return resource_path
        
        # 如果都找不到，返回原始路径（打包工具可能会处理）
        return os.path.join("resources", relative_path)
    
    def setup_ui(self):
        """设置左侧边栏界面 - VS Code风格"""
        # 固定宽度50px
        self.setFixedWidth(50)
        
        # 主布局
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 顶部图标区域
        top_layout = QVBoxLayout()
        top_layout.setSpacing(0)
        
        # 文件浏览器按钮
        self.file_explorer_btn = QPushButton()
        self.file_explorer_btn.setIcon(QIcon(self.get_resource_path("icons/folder_simple.svg")))
        self.file_explorer_btn.setIconSize(QSize(24, 24))
        self.file_explorer_btn.setFixedSize(50, 50)
        self.file_explorer_btn.setToolTip("文件浏览器")
        self.file_explorer_btn.setCheckable(True)
        self.file_explorer_btn.setChecked(True)
        self.file_explorer_btn.clicked.connect(self.on_file_explorer_toggled)
        top_layout.addWidget(self.file_explorer_btn)
        
        # 搜索按钮
        self.search_btn = QPushButton()
        self.search_btn.setIcon(QIcon(self.get_resource_path("icons/search.svg")))
        self.search_btn.setIconSize(QSize(24, 24))
        self.search_btn.setFixedSize(50, 50)
        self.search_btn.setToolTip("搜索")
        self.search_btn.setCheckable(True)
        self.search_btn.clicked.connect(self.on_search_clicked)
        top_layout.addWidget(self.search_btn)
        
        # 添加弹性空间
        top_layout.addStretch()
        
        layout.addLayout(top_layout)
        
        # 底部设置区域（可选）
        bottom_layout = QVBoxLayout()
        bottom_layout.setSpacing(0)
        
        # 设置按钮
        self.settings_btn = QPushButton()
        self.settings_btn.setIcon(QIcon(self.get_resource_path("icons/settings.svg")))
        self.settings_btn.setIconSize(QSize(24, 24))
        self.settings_btn.setFixedSize(50, 50)
        self.settings_btn.setToolTip("设置")
        self.settings_btn.setCheckable(True)
        bottom_layout.addWidget(self.settings_btn)
        
        layout.addLayout(bottom_layout)
        
        # 设置边栏样式
        self.setStyleSheet("""
            QWidget {
                background-color: #2d2d2d;
                border-right: 2px solid gray;
                border-left: 1px solid #444;
            }
            QPushButton {
                background-color: transparent;
                border: none;
                border-radius: 0px;
            }
            QPushButton:hover {
                background-color: #444;
            }
            QPushButton:pressed {
                background-color: #555;
            }
            QPushButton:checked {
                background-color: #3498db;
            }
        """)
    
    def on_file_explorer_toggled(self):
        """文件浏览器按钮点击事件"""
        is_checked = self.file_explorer_btn.isChecked()
        self.file_explorer_toggled.emit(is_checked)
    
    def on_search_clicked(self):
        """搜索按钮点击事件"""
        self.search_clicked.emit()
    
    def set_file_explorer_checked(self, checked):
        """设置文件浏览器按钮状态"""
        self.file_explorer_btn.setChecked(checked)
    
    def toggle_file_explorer(self, visible):
        """切换文件浏览器显示状态"""
        self.set_file_explorer_checked(visible)
        # 触发文件浏览器切换信号
        self.file_explorer_toggled.emit(visible)
    
    def get_stylesheet(self):
        """返回左侧边栏的样式表"""
        return ""